﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using LayuiDemo.Models;


namespace LayuiDemo.ViewModels.PostVMs
{

	public partial class PostListVM : BasePagedListVM<Post_View, PostSearcher>
	{
		public PostListVM()
		{
			DetailGridPrix = "Entity.Childs";
		}
		protected override List<GridAction> InitGridAction()
		{
			return new List<GridAction>
			{
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"", dialogWidth: 800),
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "", dialogWidth: 800),
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "", dialogWidth: 800),
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.Details, Localizer["Sys.Details"], "", dialogWidth: 800),
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.BatchEdit, Localizer["Sys.BatchEdit"], "", dialogWidth: 800),
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.BatchDelete, Localizer["Sys.BatchDelete"], "", dialogWidth: 800),
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.Import, Localizer["Sys.Import"], "", dialogWidth: 800),
				this.MakeStandardAction("Post", GridActionStandardTypesEnum.ExportExcel, Localizer["Sys.Export"], ""),
			};
		}


		protected override IEnumerable<IGridColumn<Post_View>> InitGridHeader()
		{
			return new List<GridColumn<Post_View>>{
				this.MakeGridHeader(x => x.Title).SetEditType(EditTypeEnum.TextBox),
				this.MakeGridHeader(x => x.Content).SetAllowMultiLine(true),
				this.MakeGridHeader(x => x.Url_view),
				 this.MakeGridHeader(x => x.Count).SetShowTotal(),
				  this.MakeGridHeader(x => x.Rate).SetShowTotal(),
				this.MakeGridHeaderAction(width: 200)
			};
		}

		public override IOrderedQueryable<Post_View> GetSearchQuery()
		{
			var query = DC.Set<Post>()
				.CheckContain(Searcher.Title, x => x.Title)
				.CheckContain(Searcher.Content, x => x.Content)
				.CheckEqual(Searcher.BlogId, x => x.BlogId)
				.Select(x => new Post_View
				{
					ID = x.ID,
					Title = x.Title,
					Content = x.Content,
					Url_view = x.Blog.Url,
					Count = x.Count,
					Rate = x.Rate,
				})
				.OrderBy(x => x.ID);
			return query;
		}

	}

	public class Post_View : Post
	{
		public String Url_view { get; set; }

	}
}
